package com.leesche.zcsp.node.web.controller.web.v3.device;

import com.leesche.zcsp.node.net.service.NetService;
import com.leesche.zcsp.node.utils.PropKit;
import com.leesche.zcsp.node.web.controller.web.v2.admin.WebAdminController;
import com.leesche.zcsp.node.web.service.*;
import com.leesche.zcsp.node.web.service.impl.data.read.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by kaider on 2017/2/22.
 */
@Controller
@RequestMapping("/v3/device")
public class WebDeviceV3Controller extends WebAdminController {
    private final static Logger LOGGER = LoggerFactory.getLogger(WebDeviceV3Controller.class);

    private static final String SHOW_VIEW = "web/v3/device/showView";

    @Autowired
    private PubPlaceService placeService;
    @Autowired
    private PubDeviceService deviceService;
    @Autowired
    private PubDiscountService discountService;
    @Autowired
    private PubDeviceDiscountService deviceDiscountService;
    @Autowired
    private PubDevParamService paramService;
    @Autowired
    private NetService netService;
    @Autowired
    private PubDevGiftStockService stockService;


    @RequestMapping(value = "{placeId}/placeView/{deviceId}/{cur}/deviceView", method = RequestMethod.GET)
    public String showView(
            @PathVariable("placeId") Long placeId,
            @PathVariable("deviceId") Long deviceId,
            @PathVariable("cur") int cur,
            QueryPubPlaceCondition placeCondition,
            QueryPubDeviceCondition deviceCondition,
            QueryPubDiscountCondition discountCondition,
            QueryPubDeviceDiscountCondition deviceDiscountCondition,
            QueryPubDevParamCondition paramCondition,
            Model model
    ){
        getPlaces(placeCondition,model);
        getDeviceInfo(deviceCondition,model);
        getDevices(deviceCondition,model);

        //设备套餐
        List<PubDiscountDto> discountList = getDiscountList(discountCondition, deviceDiscountCondition);
        //设备参数
        PubDevParamDto paramDto = paramService.findByCondition(paramCondition);
        model.addAttribute("placeId",placeId);
        model.addAttribute("deviceId",deviceId);
        model.addAttribute("discountList", discountList);
        model.addAttribute("paramDto", paramDto);
        model.addAttribute("cur",cur);
        return SHOW_VIEW;
    }


    /**
     * 获取场地信息
     * @param condition
     * @param model
     * @return
     */
    private Model getPlaces(
            QueryPubPlaceCondition condition,
            Model model
    ){
        Long adminId = getAdminId();
        condition.setAdminId(adminId);
        List<PubPlaceDto> list = placeService.findPubPlaceList(condition);
        model.addAttribute("placeList",list);
        return model;
    }

    /**
     * 获取设备信息
     * @param condition
     * @param model
     * @return
     */
    private Model getDeviceInfo(
            QueryPubDeviceCondition condition,
            Model model
    ){
        Long adminId = getAdminId();
        condition.setAdminId(adminId);
        InfoDeviceDto dto = deviceService.findInfoDevice(condition);
        //多局二维码
        dto.setBusinessNo(getBusinessNo());
        String payUrlTemp = PropKit.use("rqcode.properties").get("payCode1");
        String payUrl = payUrlTemp.replace("PAYCODE", dto.getMac());
        model.addAttribute("infoDevice",dto);
        model.addAttribute("payUrl", payUrl);
        return model;
    }


    /**
     * 获取设备信息
     * @param condition
     * @param model
     * @return
     */
    private Model getDevices(
            QueryPubDeviceCondition condition,
            Model model
    ){
        Long adminId = getAdminId();
        condition.setAdminId(adminId);
        condition.setPlaceId(0L);
        condition.setDeviceId(0L);
        List<InfoDeviceDto> list = deviceService.findDeviceList(condition);
        //在线设备
        List<InfoDeviceDto> online = new ArrayList<InfoDeviceDto>();
        //离线设备
        List<InfoDeviceDto> offline = new ArrayList<InfoDeviceDto>();
        //禁用设备
        List<InfoDeviceDto> disabled = new ArrayList<InfoDeviceDto>();
        //故障设备
        List<InfoDeviceDto> fault = new ArrayList<InfoDeviceDto>();
        if(list.size()>0){
            Map<String,String> onlineTemp = netService.isOnlineBatch(getDeviceNames(list));
            for(InfoDeviceDto dto : list) {
                if (onlineTemp.get(dto.getImme()) != null && onlineTemp.get(dto.getImme()).equalsIgnoreCase("ONLINE")) {
                    dto.setIsOnline(1);
                } else {
                    dto.setIsOnline(2);
                }
                if (dto.getIsOnline().equals(1) && dto.getState().equals(2)) {
                    disabled.add(dto);
                } else if (dto.getIsOnline().equals(1)) {
                    online.add(dto);
                } else if (dto.getIsOnline().equals(2)) {
                    offline.add(dto);
                } else {
                    fault.add(dto);
                }
            }
        }
        model.addAttribute("online", online);
        model.addAttribute("offline", offline);
        model.addAttribute("disabled", disabled);
        model.addAttribute("fault", fault);
        return model;
    }

    private List<String> getDeviceNames(List<InfoDeviceDto> lists) {
        List<String> deviceNames = new ArrayList<String>();
        if (lists != null) {
            for (InfoDeviceDto infoDeviceDto : lists) {
                deviceNames.add(infoDeviceDto.getImme());
            }
        }
        return deviceNames;
    }

    /**
     * 获取设备套餐列表
     * @param discountCondition
     * @param deviceDiscountCondition
     * @return
     */
    private List<PubDiscountDto> getDiscountList(
            QueryPubDiscountCondition discountCondition,
            QueryPubDeviceDiscountCondition deviceDiscountCondition
    ) {
        Long adminId = getAdminId();
        discountCondition.setAdminId(adminId);
        List<PubDiscountDto> discountList = discountService.findDiscountList(discountCondition);
        List<PubDiscountDto> discountListTemp = new ArrayList<PubDiscountDto>();
        for (PubDiscountDto discountDto : discountList) {
            deviceDiscountCondition.setDiscountId(Long.valueOf(discountDto.getDiscountid()));
            Integer flag = deviceDiscountService.selectCheckDeviceDiscount(deviceDiscountCondition);
            if (flag == 1) {
                discountDto.setFlag(1);
            } else {
                discountDto.setFlag(0);
            }
            discountListTemp.add(discountDto);
        }
        return discountListTemp;
    }

}
